/* $RCSFile: FlashEdit.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/FlashModule/FlashEdit.aspx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:20  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:54  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice.Settings;
using AddonNice.Security;
using AddonNice.UI;
using AddonNice.Helpers;
using AddonNice.Configuration;
using AddonNice.Diagnostics;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.FlashModule
{
	/// <summary>
	/// 
	/// </summary>
	public partial class FlashEdit: AddonNice.UI.AddEditItemPage
	{
		ImageButton cmdshowGalleryButton;
		ImageButton cmdshowGalleryButton2;

        protected void Page_Load(object sender, System.EventArgs e) 
		{    
			// Form the script that is to be registered at client side.
			string scriptString = "function newWindow(file,window) {";
			scriptString += "msgWindow=open(file,window,'resizable=yes,width=800,height=600,scrollbars=yes');";
			scriptString += "if (msgWindow.opener == null) msgWindow.opener = self;";
			scriptString += "}";
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FlashEdit Page_Load scriptString: {0}.", scriptString),PageTraceSwitch.Sw.Info);

			Type cs							=	GetType();
			if(!IsPageClientScriptBlockRegistered(cs,"newWindow"))
				RegisterPageClientScriptBlock(cs,"newWindow", scriptString,true);

			//showGalleryButton.NavigateUrl	= string.Format("javascript:newWindow('UploadFlash.aspx?FieldID={0}&Mid={1}','gallery')",Src.ClientID,ModuleID);
            string strCmd   =   string.Format("javascript:newWindow('UploadFlash.aspx?FieldID={0}&Mid={1}','gallery')",Src.ClientID,ModuleID);
			cmdshowGalleryButton.OnClientClick	= strCmd;
			cmdshowGalleryButton2.OnClientClick	= strCmd;
      
			if ( !IsPostBack ) 
			{
				if ( ModuleID > 0 ) 
				{
					// Get settings from the database
					if ( moduleSettingsDic.ContainsKey(flStr.FlashSrc) )
						Src.Text			=  moduleSettingsDic[flStr.FlashSrc].ToString();
					if ( moduleSettingsDic.ContainsKey(flStr.FlashWidth) )
						Width.Text			=  moduleSettingsDic[flStr.FlashWidth].ToString();
					if ( moduleSettingsDic.ContainsKey(flStr.FlashHeight) )
						Height.Text			= moduleSettingsDic[flStr.FlashHeight].ToString();
                    if (moduleSettingsDic.ContainsKey(flStr.FlashBackColor))
                    {
                        RadColorPicker1.SelectedColor= ColorTranslator.FromHtml(moduleSettingsDic[flStr.FlashBackColor].ToString());
                    }
				}
			}
		}

		/// <summary>
		/// Set the module guids with free access to this page
		/// </summary>
		protected override ArrayList AllowedModules
		{
			get
			{
				ArrayList al = new ArrayList();
				al.Add ("623EC4DD-BA40-421c-887D-D774ED8EBF02");
				return al;
			}
		}

		/// <summary>
		/// Save the settings to the ModuleConfiguration database table.  It  uses the 
		/// AddonNice.DesktopModulesDB() data component to encapsulate the data 
		/// access functionality.
		/// </summary>
		/// <param name="e"></param>
        void FlashEdit_Update(object sender, AddonNice.UI.ADNPageEventArgs e)
		{
			// Update settings in the database
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FlashEdit FlashEdit_Update Src.Text: {0}.", Src.Text),PageTraceSwitch.Sw.Info);
			ModulesDB mDB		    =	new ModulesDB();
            try
            {
                string flashPath    =   moduleSettingsDic[flStr.FlashPath].FullPath;
                string src          =   Src.Text;
                if (src.StartsWith(flashPath))
                    src             =   src.Substring(flashPath.Length);
                SettingItem it      =   moduleSettingsDic[flStr.FlashSrc];
                it.Value            =   src;
                it.UpdateModuleSetting(mDB);
                
                it                  =   moduleSettingsDic[flStr.FlashHeight];
                it.Value            =   Height.Text;
                it.UpdateModuleSetting(mDB);
                
                it                  =   moduleSettingsDic[flStr.FlashWidth];
                it.Value            =   Width.Text;
                it.UpdateModuleSetting(mDB);

                it                  =   moduleSettingsDic[flStr.FlashBackColor];
                it.Value            =   ColorTranslator.ToHtml(RadColorPicker1.SelectedColor);
                it.UpdateModuleSetting(mDB);
            }
            catch (Exception ex)
            {
			    if (PageTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FlashEdit FlashEdit_Update Src.Text: {0}, ex: {1}", Src.Text,ex),PageTraceSwitch.Sw.Info);
                throw;
            }
		}
      
		#region Std overrides

        override protected void OnPreInit(EventArgs e)
		{
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]FlashEdit OnPreInit Begin",PageTraceSwitch.Sw.Info);
			string strLoc	                        =	LocalizeHelper.GetCurrentNeutralCultureName();
			cmdshowGalleryButton					=	new ImageButton();
			cmdshowGalleryButton.SkinID			    =	"adn_opengallery_"+strLoc;
			cmdshowGalleryButton.AlternateText		=	LocalizeHelper.Localize(flStr.SHOW_FLASH_GALLERY,"Show Flash Gallery");
			cmdshowGalleryButton.CausesValidation	=	false;
			cmdshowGalleryButton2					=	new ImageButton();
			cmdshowGalleryButton2.SkinID			=	"adn_opengallery_"+strLoc;
			cmdshowGalleryButton2.AlternateText		=	LocalizeHelper.Localize(flStr.SHOW_FLASH_GALLERY,"Show Flash Gallery");
			cmdshowGalleryButton2.CausesValidation	=	false;
            base.OnPreInit(e);
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]FlashEdit OnPreInit End",PageTraceSwitch.Sw.Info);
        }

		protected override void OnInit(EventArgs e)
		{  
			InitializeComponent();
			base.OnInit(e);
            placeHolderForButtons.Controls.Add(new LiteralControl("&#160;"));
			placeHolderForButtons.Controls.Add(cmdshowGalleryButton);
            placeHolderForButtons2.Controls.Add(new LiteralControl("&#160;"));
			placeHolderForButtons2.Controls.Add(cmdshowGalleryButton2);

		}

		private void InitializeComponent() 
		{    
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "FlashEdit InitializeComponent Begin",PageTraceSwitch.Sw.Info);
			Load		+=	new EventHandler(Page_Load);
            Update      +=  new PageEventHandler(FlashEdit_Update);
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "FlashEdit InitializeComponent End",PageTraceSwitch.Sw.Info);
		}

        
		protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons
		{
			get	{ return PlaceHolderButtons; }
			set {}
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons2
		{
			get	{ return PlaceHolderButtons2; }
			set {}
		}

		#endregion
	}
}
